package com.TP.www.servlet;

import com.TP.www.po.Appraise;
import com.TP.www.po.Coupon;
import com.TP.www.po.Good;
import com.TP.www.po.Store;
import com.TP.www.service.AppraisePort;
import com.TP.www.service.CouponPort;
import com.TP.www.service.GoodPort;
import com.TP.www.service.StorePort;
import com.TP.www.service.impl.CouponImpl;
import com.TP.www.service.impl.StoreImpl;

import java.util.List;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 */
public class StoreMenu {

    /**
     * 商家店铺展示主函数
     */
    public static void showStoreForMerchantMain(){
        while (true) {
            String choose;
            String lin = "0";
            List<Store> stores = StorePort.getStoreByMerchantId(SELECT_FROM_STORE_BY_MERCHANT_ID, loginUser.getId());
            if (stores.isEmpty()) {
                System.out.println("您还没有店铺哝(^.^)");
                System.out.println("输入0以创建店铺!(其他键返回上一级)");
                choose = SYS_SC.nextLine();
                if (lin.equals(choose)) {
                    new StoreImpl().creatStore();
                    return;
                }
            }
            Store store = stores.get(0);
            List<Good> goods = GoodPort.getGoodByStoreId(SELECT_GOODS_BY_STORE_ID, store.getId());
            List<Coupon> coupons = CouponPort.getCouponByStoreId(GET_COUPON_BY_STORE_ID, store.getId());
            List<Appraise> appraises = AppraisePort.getAppraiseByStoreId(GET_APPRAISE_BY_STORE_ID, store.getId());
            System.out.println("**************店铺界面*************");
            System.out.println("店铺名: " + store.getStoreName());
            System.out.println("店铺简介: " + store.getIntro());
            System.out.println("营业额: " + store.getAsset());
            if (!coupons.isEmpty()) {
                System.out.println("优惠卷:");
                for (Coupon coupon : coupons) {
                    System.out.println("满" + coupon.getInitialNum() + "减" + coupon.getOffNum());
                }
            }
            if (!appraises.isEmpty()) {
                System.out.println("店铺评论:");
                for (Appraise appraise : appraises) {
                    System.out.println("用户: " + appraise.getName() + " " + appraise.getContent());
                }
            }
            System.out.println("****************商品界面*************");
            if (goods.isEmpty() || GoodsMenu.judge(goods)) {
                System.out.println("暂无商品....");
            } else {
                for (Good good : goods) {
                    if (good.getState() == 1) {
                        System.out.println("商品名: " + good.getGoodName() + " 单价: " + good.getPrice());
                    }
                }
            }
            System.out.println("**************************************");
            System.out.println("*\t\t0.返回上级\t\t\t\t*");
            System.out.println("*\t\t1.修改店铺信息\t\t\t*");
            System.out.println("*\t\t2.修改优惠卷信息\t\t\t\t*");
            System.out.println("*\t\t3.修改菜品信息\t\t\t\t*");
            System.out.println("*\t\t4.店铺评论\t\t\t\t*");
            System.out.println("*********************************");
            System.out.println("请输入数字指令：");
            choose = SYS_SC.nextLine();
            switch (choose) {
                case "0":
                    return;
                case "1":
                    new StoreImpl().modifyStore();
                    break;
                case "2":
                    CouponMenu.show(store.getId());
                    break;
                case "3":
                    GoodsMenu.show(store.getId());
                    break;
                case "4":
                    AppraiseMenu.showAppraiseMain();
                    break;
                default:
                    System.out.println("指令输入有误！请重新输入：");
            }
        }

    }

    /**
     * 顾客浏览店铺主函数
     */
    public static void showStoreForCustomerMain(){
        List<Store> stores = StorePort.getStore(SELECT_FROM_STORE_AND_MERCHANT);
        while (true) {
            int i = 1;
            String choose;
            System.out.println("*********************************");
            System.out.println("以下为当前所有店铺:");
            for (Store store : stores) {
                System.out.println(i + " " + store.getStoreName() + " " + store.getIntro());
                i++;
            }
            System.out.println("*********************************");
            System.out.println("请输入店铺序号(按0返回上级界面):");
            choose = SYS_SC.nextLine();
            if (Integer.parseInt(choose) == 0) {
                return;
            }
            while (Integer.parseInt(choose) >= i || Integer.parseInt(choose) < 0) {
                System.out.println("输入有误!");
                System.out.println("请重新输入店铺序号(按0返回上级界面):");
                if (Integer.parseInt(choose) == 0) {
                    return;
                }
            }
            showForCustomer(stores.get(Integer.parseInt(choose) - 1));
        }
    }

    /**
     * 展示指定店铺给顾客
     * @param store 指定店铺
     */
    public static void showForCustomer(Store store){
        List<Good> goods = GoodPort.getGoodByStoreId(SELECT_GOODS_BY_STORE_ID,store.getId());
        List<Appraise> appraises = AppraisePort.getAppraiseByStoreId(GET_APPRAISE_BY_STORE_ID,store.getId());
        List<Coupon> coupons = CouponPort.getCouponByStoreId(GET_COUPON_BY_STORE_ID, store.getId());
        while (true) {
            int i = 1;
            String choose;
            System.out.println("*********************************");
            System.out.println("欢迎光临" + store.getStoreName());
            if (!appraises.isEmpty()){
                System.out.println("店铺评价: ");
                for(Appraise appraise:appraises){
                    System.out.println("用户" + appraise.getName() + " :" + appraise.getContent());
                }
                System.out.println("*********************************");
            }
            if (!coupons.isEmpty()) {
                System.out.println("优惠卷:");
                for (Coupon coupon : coupons) {
                    System.out.println("满" + coupon.getInitialNum() + "减" + coupon.getOffNum());
                }
                System.out.println("*********************************");
            }

            if(goods.isEmpty()){
                System.out.println("当前店铺无上架商品!");
                System.out.println("按任意键返回...");
                SYS_SC.nextLine();
                return;
            }else {
                for (Good good:goods) {
                    if(good.getState() == 1){
                        System.out.println(i + " " + good.getGoodName() + " " + good.getPrice());
                        i++;
                    }
                }
            }
            System.out.println("*********************************");
            System.out.println("\t\t0.返回上级\t\t");
            System.out.println("\t\t1.一键领取优惠卷\t\t");
            System.out.println("\t\t2.加入购物车\t\t");
            choose = SYS_SC.nextLine();
            switch (choose){
                case "0":
                    return;
                case "1":
                    new CouponImpl().getCoupon(coupons);
                    break;
                case "2":
                    new StoreImpl().storeAddCart(goods,i);
                    break;
                default:
                    System.out.println("输入有误!请重新输入...");
            }
        }
    }
}
